//
//  CategoryDataModel.m
//  YZZX
//
//  Created by Easaa on 15/4/20.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "CategoryDataModel.h"

#import "LFURlRequest.h"
#import "LFDataRequest.h"
#import "JsonUtil.h"
#import "HXDataBaseObject.h"


@implementation CategoryDataModel


-(id)initWithDataDic:(NSDictionary *)dic
{
    self=[super init];
    if (self) {

        
        self.image2_url =[dic objectForKey:@"image2_url"];
        self.cate_name  =[dic objectForKey:@"cate_name"];
        self.children   =[dic objectForKey:@"children"];
        self.cate_id    =[[dic objectForKey:@"cate_id"] stringValue];
        self.page_type  =[dic objectForKey:@"page_type"];
        self.image_url  =[dic objectForKey:@"image_url"];
        self.config     =[dic objectForKey:@"config"];
        self.cate_type  =[dic objectForKey:@"cate_type"];
        
        
    }
    return self;
    

}

+ (void)categoryListRequestWithWebsiteID:(NSString *)strWetsiteID
                          complateAction:(void (^)(NSArray *cateList, NSString *errMsg))completeAction
{
    
    NSURLRequest *urlRequest = [LFURlRequest getUrlRequestWithMethod:YZ_CATEGORY_LIST
                                                           andParams:@{@"user_id":[XSZ_UICommon useridChangUaerdfLoginBoolString],
                                                                       @"site_id": strWetsiteID}];
    [LFDataRequest dataRequestWith:urlRequest complete:^(id responseData, NSError *error) {
        if (error)
        {
            completeAction(nil, error.localizedDescription);
        } else if (responseData)
        {
            if ([responseData isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *dic = responseData;
                if ([[dic objectForKey:@"is_success"] boolValue])
                {
                    NSString *strData = [dic objectForKey:@"data"];
                    if (!StringIsEmpty(strData))
                    {
                        //缓存栏目
                        [CategoryDataModel cacheCategoryWithData:strData withWebsiteID:strWetsiteID];
                        
                        NSArray *cateList = [JsonUtil jsonToArray:strData];
                        NSArray *cateModelList = [CategoryDataModel convertToHXCategoryListModel:cateList];
                        completeAction(cateModelList, nil);
                    }
                } else
                {
                    completeAction(nil, [dic objectForKey:@"message"]);
                }
            }
        }
    }];
}

/**
 *  将网站栏目数据转换成HXCategoryListModel
 *
 *  @param ary 栏目数据
 *
 *  @return 装有HXCategoryListModel的array
 */
+ (NSArray *)convertToHXCategoryListModel:(NSArray *)ary
{
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:ary.count];
    for (NSDictionary *firstDic in ary)
    {
        CategoryDataModel *firstCategoryListModel = [[CategoryDataModel alloc] initWithDataDic:firstDic];
        
        id chirldren = [firstDic objectForKey:@"children"];
        if (chirldren != nil && chirldren != [NSNull null])
        {
            NSArray *childrens = [self convertToHXCategoryListModel:chirldren];
            firstCategoryListModel.children = childrens;
        }else if (chirldren == nil||chirldren == [NSNull null])
        {
        firstCategoryListModel.children =nil;
        }
        
        [mArray addObject:firstCategoryListModel];
    }
    return mArray;
}

/**
 *  缓存栏目
 */
+ (void)cacheCategoryWithData:(NSString *)strCategory withWebsiteID:(NSString *)strWebsiteID
{
    HXDataBaseObject *dataBaseObject = [HXDataBaseObject shareDataBaseObject];
    if ([dataBaseObject openDataBase])
    {
        NSString  *strCateCacheKey = [NSString stringWithFormat:@"Category_Cache_%@", strWebsiteID];
        if (StringIsEmpty([dataBaseObject queryDataWithKey:strCateCacheKey
                                                   inTable:_HX_APP_CACHE_DATABASE_TABLE_NAME__]))
        {//数据库中没有缓存栏目
            [dataBaseObject insertData:@[@{DataBaseValueKey: strCateCacheKey,
                                           DataBaseValue: strCategory}]
                               toTable:_HX_APP_CACHE_DATABASE_TABLE_NAME__];
        } else
        {//数据库中已经缓存栏目， 则更新栏目缓存
            [dataBaseObject updateData:@[@{DataBaseValueKey: strCateCacheKey,
                                           DataBaseValue: strCategory}]
                               toTable:_HX_APP_CACHE_DATABASE_TABLE_NAME__];
        }
    }
    [dataBaseObject closeDataBase];
}

/**
 *  获取栏目缓存
 */
+ (NSArray *)getCategoryListWithWebsiteID:(NSString *)strWebID
{
    NSString *strCate = nil;
    HXDataBaseObject *dataBaseObject = [HXDataBaseObject shareDataBaseObject];
    if ([dataBaseObject openDataBase])
    {
        NSString  *strCateCacheKey = [NSString stringWithFormat:@"Category_Cache_%@", strWebID];
        strCate = [dataBaseObject queryDataWithKey:strCateCacheKey inTable:_HX_APP_CACHE_DATABASE_TABLE_NAME__];
    }
    [dataBaseObject closeDataBase];
    NSArray *cateList = nil;
    if (!StringIsEmpty(strCate))
    {
        cateList = [JsonUtil jsonToArray:strCate];
        cateList = [CategoryDataModel convertToHXCategoryListModel:cateList];
    }
    return cateList;
}




@end
